package com.qf.service.impl;

import com.alibaba.excel.EasyExcel;
import com.qf.listener.OutpatientRechargeListener;
import com.qf.model.entity.OutpatientRecharge;
import com.qf.dao.OutpatientRechargeDao;
import com.qf.service.OutpatientRechargeService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (OutpatientRecharge)表服务实现类
 *
 * @author makejava
 * @since 2024-04-30 10:54:21
 */
@Service("outpatientRechargeService")
public class OutpatientRechargeServiceImpl implements OutpatientRechargeService {
    @Autowired
    private OutpatientRechargeDao outpatientRechargeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OutpatientRecharge queryById(Integer id) {
        return this.outpatientRechargeDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<OutpatientRecharge> queryByPage(Integer pageNum) {
        return this.outpatientRechargeDao.queryAllByLimit();
    }

    @Override
    public List<OutpatientRecharge> selectOutpatientRechargeByCondition(Map<String, Object> map) {
        return outpatientRechargeDao.selectOutpatientRechargeByCondition(map);
    }

    @Override
    public List<OutpatientRecharge> findAllOutpatientRecharge() {
        return outpatientRechargeDao.findAllOutpatientRecharge();
    }

    /**
     * 新增数据
     *
     * @param outpatientRecharge 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(OutpatientRecharge outpatientRecharge, HttpSession session) {

        // 如果添加项有创建人：处理创建人(从session中获取当前用户)，
        // SysUser user = (SysUser) session.getAttribute("user");
        // 创建时间(new Date)
        // 警告：一定要先登录 ，才能从session中获取数据

        OutpatientRecharge opr = (OutpatientRecharge) session.getAttribute("outpatientRecharge");
        opr.setOperationTime(new Date());

        int num = outpatientRechargeDao.insert(outpatientRecharge);

        if (num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insertBatch(List<OutpatientRecharge> list) {
        int num = outpatientRechargeDao.insertBatch(list);
        if (num > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改数据
     *
     * @param outpatientRecharge 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(OutpatientRecharge outpatientRecharge) {
        int num = outpatientRechargeDao.update(outpatientRecharge);
        if (num > 0) {
            return true;
        }
        return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.outpatientRechargeDao.deleteById(id) > 0;
    }

    @Override
    public boolean deleteBatch(List<Integer> list) {
        return outpatientRechargeDao.deleteBatch(list);
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        // 1. 先查询所有信息
        List<OutpatientRecharge> list = outpatientRechargeDao.findAllOutpatientRecharge();

        // 2. 导出excel文件

        /**
         * write("文件地址",OutpatientRecharge.class是指定表头)
         *                      --OutpatientRecharge中有注解将 属性和表头的列映射
         * .sheet("门诊充值信息") 指定 excel 文件中那个表
         * .doWrite(list)写list 数组
         */

        try {
            EasyExcel.write(response.getOutputStream(), OutpatientRecharge.class).
                    sheet("门诊充值信息").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean importExcel(MultipartFile file) {
        // MultipartFile file得到文件，调用read方法
        try {
            // 调用listener批量导入数据库中

            /**
             * 读取excel 表格
             * read("文件名", OutpatientRecharge.class 表头, new OutpatientRechargeListener()：导入监听器)
             * .sheet("门诊充值信息") 指定读取那个表格
             * .doReadSync(); 异步读取,返回数组List
             */

            List<OutpatientRecharge> list = EasyExcel.read(file.getInputStream(), OutpatientRecharge.class,
                            new OutpatientRechargeListener(this)).
                    sheet("门诊充值信息").doReadSync();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
